//订单管理--订单列表
App.util.ns('Admin.Orders.Orders');
App.pages.add((function(){
    var NS = Admin.Orders.Orders;
    var page = new App.Page({
        name: 'orders/orders/index'
    });
    //页面初始化
    page.initStack.add('dataTable', function(){
        //面包屑导航
        this.setBreadcrumb([{
            icon: '<i class="fa fa-dollar"></i>',
            content: '订单管理'
        }, {
            content: '订单列表'
        }]);
        this.dataTable = new App.DataTable({
            baseUrl: '/admin/orders/orders',
            dataUrl: 'lists',
            table: {
                id: 'id',
                main: 0,
                showId: true,
                columns: App.util.copy(NS.Conf.columns)
            },
            filter: [
                {type: 'keywords'},
                {type: 'timeRange'},
                // {type: 'select', name: 'invoice_status', title: '发票状态', width: '100px', items: [
                //     [0, '不需要'],
                //     [1, '需要']
                // ]},
                {type: 'select', name: 'type', title: '交易类型', width: '100px', items: [
                    [1, '充值'],
                    [2, '交押金'],
                    [3, '退押金'],
                    [4, '商城订单支付'],
                    [5, '商城订单退款']
                ]},
                {type: 'select', name: 'status', title: '订单状态', width: '90px', items: [
                    [1, '待支付'],
                    [2, '支付完成'],
                    [3, '订单完成'],
                    [4, '失败'],
                ]},
                {type: 'select', name: 'deliver_status', title: '快递状态', width: '90px', items: [
                    [0, '-- --'],
                    [1, '填充完毕'],
                    [2, '订单已签收']
                ]}
            ],
            menu: [
                //{parent: 'add', permission: 'op:admin.orders.orders.create'},
                {parent: 'view', permission: 'op:admin.orders.orders.detail'},
                //{parent: 'edit', permission: 'op:admin.orders.orders.update'},
                {
                    type: 'inline',
                    name: '运单填写',
                    permission: 'gp:admin/orders/orders',
                    conditions: [['type', '4']],
                    operation: function(dataTable, orders){
                        var win = dataTable.wins.get('deliversno');
                        win.setTitle('运单填写 - '+orders.order_name);
                        win.form.change({
                            order_id: orders.id,
                            user_id: orders.user_id,
                            deliver_no:orders.deliver_no,
                            deliver_code:orders.deliver_code
                        });
                        if(orders.deliver){
                            win.form.formItems['deliver_code'].change(orders.deliver.id, orders.deliver.name, orders.deliver);
                        }else{
                            win.form.formItems['deliver_code'].reset();
                        }
                        win.show();
                    }
                },
                {parent: 'drop', permission: 'op:admin.orders.orders.delete'},
                {parent: 'multiDrop', permission: 'op:admin.orders.orders.multiDelete'},
                {parent: 'oplog', permission: 'op:admin.orders.orders.getOpLogs'},
                {type: 'inline', permission: 'op:admin.orders.orders.complete',
                    name: '订单完成', operation: {type: 'change', url: 'complete'},
                    conditions: [['status', '2'],['type', '4']]
                }
            ],
            detail: {
                form: {
                    items: App.util.copy(NS.Conf.form),
                    onChange: function(form, data){
                        var htmlinfo = '';
                        var htmljson = '';
                        var order_info=data.order_info;
                        var consignee_info=data.consignee_info;
                        if(!order_info){
                            htmlinfo = '暂无信息';
                        }else{
                            order_info = JSON.parse(order_info);
                            if(order_info.constructor!== Array){
                                htmlinfo = '暂无信息';
                            }
                            for (var i =0 ;i<order_info.length;i++){
                                htmlinfo += '商品名称：'+order_info[i].name;
                                if(order_info[i].specStr){
                                    htmlinfo += '，规格：'+order_info[i].specStr;
                                }else{
                                    htmlinfo += '，规格：默认';
                                }
                                htmlinfo += '，数量：'+ order_info[i].num+
                                '，价格：'+order_info[i].price;
                                htmlinfo += '<br>';
                            }
                        }
                        if(!consignee_info){
                            htmljson = '暂无信息';
                        }else{
                            consignee_info = JSON.parse(consignee_info);
                            htmljson += '收货地址：'+consignee_info.cityName+consignee_info.countyName+consignee_info.detailInfo+'<br>';
                            htmljson += '手机号：'+consignee_info.telNumber+'<br>';
                            htmljson += '昵称：'+consignee_info.userName;
                        }
                        form.formItems['order_info'].change(htmlinfo);
                        form.formItems['consignee_info'].change(htmljson);

                        // if(data.type+'' === '7'){
                        //     var orderInfo = data.orderInfoText;
                        //     var consigneeInfo = JSON.parse(data.consignee_info);
                        //     var orderInfoText = '';
                        //     for(var i in orderInfo){
                        //         var goodsName = orderInfo[i].goodsName;
                        //         var goodsNum = orderInfo[i].num;
                        //         var goodsPrice = orderInfo[i].price;
                        //         orderInfoText += goodsName +' '+ goodsNum + ' 件 ' + goodsPrice + '元<br>';
                        //     }
                        //     var userName = consigneeInfo.userName;
                        //     var telNumber = consigneeInfo.telNumber;
                        //     var userddress = consigneeInfo.provinceName + consigneeInfo.cityName + consigneeInfo.countyName + consigneeInfo.detailInfo;
                        //     var consigneeInfoText = userName + "<br>" + telNumber + "<br>" + userddress;
                        //     form.formItems['order_details'].change(orderInfoText ? orderInfoText : '');
                        //     form.formItems['consignee_details'].change(consigneeInfoText ? consigneeInfoText : '');
                        // }
                    }
                }
            },
            after: function(dataTable){
                dataTable.wins.add(new NS.Deliversno(dataTable), 'deliversno');
            }
        }, page);
        this.dataTable.fix(this.contentContainer);
        this.dataTable.loadData();
    });
    //页面刷新
    page.refreshStack.add('dataTable', function(){
        this.dataTable.loadData(true);
    });
    //页面销毁
    page.destroyStack.add('dataTable', function(){
        this.dataTable.destroy();
    });
    return page;
})());